/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.staf.NetworkReportGenerator;

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Iterator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import org.xml.sax.SAXException;

/**
 *
 * @author aezhov
 */
public class SwitchList {

    LinkedHashMap<String, Switch> swList = new LinkedHashMap<String, Switch>();
    String pgsqlDatabase = null;
    String pgsqlUsername = null;
    String pgsqlPass = "";
    boolean useDatabase = false;
    Connection dbConnect = null;
    NmapXMLParser nmapParser = null;

    public Iterator<Switch> buildSwitchlist() throws
            java.io.FileNotFoundException, java.io.IOException,
            ConfigurationException, org.xml.sax.SAXException
    {
        LineNumberReader strReader = new LineNumberReader(new FileReader("/etc/netmonviewer.properties"));
        String confLine = "";
        while ((confLine = strReader.readLine()) != null) {
            String[] tokens = confLine.split("[=.]");
            if (tokens.length < 3) {
                continue;
            }

            System.out.println("Tokens " + tokens[0] + " " + tokens[1] + " " + tokens[2]);
            if (tokens[0].equals("pgsql")) {
                if (tokens[1].equals("database")) {
                    pgsqlDatabase = tokens[2];
                } else if (tokens[1].equals("user")) {
                    pgsqlUsername = tokens[2];
                } else if (tokens[1].equals("pass")) {
                    pgsqlPass = tokens[2];
                }
            } else if (tokens[0].equals("nmap")) {
                if (tokens[1].equals("output")) {
                    nmapParser = new NmapXMLParser(tokens[2]);
                }
            } else {
                Switch sw = swList.get(tokens[0]);
                if (sw == null) {
                    sw = new Switch(tokens[0]);
                    swList.put(tokens[0], sw);
                }
                sw.addConfig(tokens[1], tokens);
            }
        }

         // Make database connection
        if (pgsqlUsername == null || pgsqlDatabase == null) {
            System.out.println("=== WARNING ! Working without database");
            useDatabase = false;
        } else {
            String connectString = "jdbc:postgresql:" + pgsqlDatabase;
            try {
                Class.forName("org.postgresql.Driver");
                dbConnect = DriverManager.getConnection(connectString, pgsqlUsername, pgsqlPass);
                useDatabase = true;
            } catch (java.sql.SQLException exc) {
                useDatabase = false;
                System.out.println("=== WARNING ! Working without database " + exc);
            } catch (ClassNotFoundException exc) {
                System.out.println("=== WARNING ! Working without database: jdbc driver not found");
            }
        }

        Set keys = swList.keySet();
        Iterator<String> it = keys.iterator();
        while (it.hasNext()) {
            Switch sw = swList.get(it.next());
            sw.commitConfig(dbConnect);
        }

        return swList.values().iterator();
    }


    public boolean useDatabase() {
        return useDatabase;
    }

    public NmapXMLParser getNmapParser() {
        return nmapParser;
    }
}
